package com.itheima.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * Consumer 限流机制:
 *  1、确保ack机制为手动确认签收模式
 *  2、在标签 rabbit:listener-container 配置属性prefetch="" [单次可以拉取消息的最大值]
 *      如 prefetch="1"，表示消费端每次从mq拉取一条消息来消费,直到手动确认消费完毕后,才会拉取下一条消息
 */
@Component
public class QosListener implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        // 1、获取消息
        System.out.println("接收到消息:" + (new String(message.getBody())));

        // 2、处理业务逻辑
        // 3、手动签收
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag,true);
    }
}
